// submit-order.js
var util = require('../../utils/request.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        storeId: '',
        shoppingCart: {},
        name: '',
        mobile: '',
        memo: '',
        street: '',
        order_time: '点击选择送达时间'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let shoppingCart = wx.getStorageSync('shoppingCart');
        var parm = JSON.parse(shoppingCart)
        this.setData({
            storeId: options.storeId,
            shoppingCart: parm
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        getApp().http.request({
            url: '/api/v1/user/address',
            method: 'GET',
            data: {},
            dataType: 'json',
            success: function (res) {
                console.log(res.data.data.mobile)
                that.setData({
                    name: res.data.data.name,
                    mobile: res.data.data.mobile,
                    memo: '',
                    street: res.data.data.street,
                });
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },


    booking_seats: function () {
        wx.navigateTo({
            url: "../booking-seats/booking-seats?id=" + parm.id
        })
    },
    booking_order: function () {
        wx.navigateTo({
            url: "../goods-list/goods-list?id=" + parm.id
        })
    },
    booking_takeaway: function () {
        wx.navigateTo({
            url: "../goods-list/goods-list?id=" + parm.id
        })
    },
    nameEvent: function (e) {
        this.setData({
            name: e.detail.value
        })
    },
    mobileEvent: function (e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    streetEvent: function (e) {
        this.setData({
            street: e.detail.value
        })
    },
    orderTimeEvent: function (e) {
        this.setData({
            order_time: e.detail.value
        })
    },
    /**
     * 保存订单
     */
    submit: function () {
        if (this.data.name != '' && this.data.mobile != '' && this.data.street != '' && this.order_time != '点击选择送达时间') {
            if (getApp().globalData.rex.test(this.data.mobile)) {
                var that = this;
                var path = '/api/v1/order/takeaway?';
                var product = ''
                for (var i = 0; i < this.data.shoppingCart.list.length; i++) {
                    if (parseInt(this.data.shoppingCart.list[i].num) > 0) {
                        product += 'id=' + this.data.shoppingCart.list[i].data.id + '&qty=' + this.data.shoppingCart.list[i].num + ','
                    }
                }
                path += 'name=' + this.data.name + '&mobile=' + this.data.mobile + '&memo=' + this.data.memo + '&street=' + this.data.street + '&order_time=' + this.data.order_time + '&product=' + encodeURIComponent(product) + '&merchant_id=' + this.data.storeId
                getApp().http.request({
                    url: path,
                    method: 'POST',
                    data: {},
                    dataType: 'json',
                    success: function (res) {

                        setTimeout(function () {
                            wx.showToast({
                                title: '下单成功!等待商家确认.',
                                duration: 2000
                            })
                        }, 1000)
                        wx.navigateBack({
                            delta: 2
                        })
                        var userShoppingCart = getApp().globalData.shoppingCart
                        //订单保存成功后清理购物车
                        for (var i = 0; i < userShoppingCart.length; i++) {
                            if (that.data.storeId == userShoppingCart[i].mid) {
                                getApp().cleanShoppingCart(that.data.storeId, '外卖')
                            }
                        }
                    }
                })
            }
            else {
                wx.showToast({
                    title: '请输入正确的手机号码',
                    icon: '',
                    image: '',
                    duration: 2000,
                    mask: true,
                    success: function (res) {
                    },
                    fail: function (res) {
                    },
                    complete: function (res) {
                    },
                })
            }
        }
        else {
            wx.showToast({
                title: '请填写完整信息',
                duration: 2000
            })
        }
    },
    bindTimeChange: function (e) {
        this.setData({
            order_time: e.detail.value
        })
    }
})